package mobi.greenplate.client.handler;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import mobi.greenplate.client.converter.AddressConverter;
import mobi.greenplate.client.domain.Address;
import mobi.greenplate.client.parser.DomainHandler;

/**
 *
 * @author dmakariev
 */
public class AddressListHandler extends DomainHandler<List<Address>> {

    private static final String ADDRESS_TAG = "address";
    private static final String ADDRESS_BODY = "//address-list/address";
    private final List<Address> addressList = new ArrayList<Address>();
    private Address currentAddress;

    @Override
    public List<Address> getEntity() {
        return Collections.unmodifiableList(addressList);
    }

    @Override
    public void endTag(String parentPath, String tagName, String text) {
        if (parentPath.equalsIgnoreCase(ADDRESS_BODY)) {
            currentAddress = AddressConverter.endTag(currentAddress, tagName, text);
        }
    }

    @Override
    public void startTag(String parentPath, String tagName) {
        if (tagName.equalsIgnoreCase(ADDRESS_TAG)) {
            currentAddress = new Address();
            addressList.add(currentAddress);
        }
    }
}
